package servlet;

import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MainFlashServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("MainFlash");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        List<String[]> essayList=new ArrayList();
        List<String[]> essayLabelList=new ArrayList();
        String[] topLabel=new String[5];
        String[] topEssay=new String[5];
        try {
            Connection conn=null;
            PreparedStatement ptmt=null;
            ResultSet rs=null;
            Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
            String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
            conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库
            String  sql="select e.*,u.username\n" +
                    "from essay e join `user` u\n" +
                    "on e.uid=u.uid\n";
            ptmt=conn.prepareStatement(sql);
            rs = ptmt.executeQuery();
            if(rs==null){
                System.out.println("文章数据加载失败");
                request.getSession().setAttribute("status","0");
                request.getSession().setAttribute("msg","文章数据加载失败");
            }else {

                while (rs.next()){
                    String[] data=new String[10];
                 //  System.out.println(rs.getInt(1)+"|"+rs.getInt("uid")+"|"+rs.getString("title")+"|"+rs.getString("eclass")+"|"+rs.getTimestamp("create_time")+"|"+rs.getTimestamp("update_time")+"|"+rs.getInt("read_sum")+"|"+rs.getInt("zan_sum")+"|"+rs.getString("username"));
                    for(int i=0;i<10;i++){
                        if(rs.getObject(i+1)==null){
                            data[i]="";
                        }else {
                            data[i]=rs.getObject(i+1).toString();
                        }
                    }
                for (String s:
                     data) {
                    System.out.print("|"+s+"|");
                }
                System.out.println();
                    essayList.add(data);
                    System.out.println("文章数据加载成功");



                }


            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            String  sql1="SELECT e.eid,l.lid,l.word\n" +
                    "FROM le e JOIN label l\n" +
                    "on l.lid=e.lid";
            ptmt=conn.prepareStatement(sql1);
            rs = ptmt.executeQuery();
            if(rs==null){
                System.out.println("文章标签数据加载失败");
                request.getSession().setAttribute("status","0");
                request.getSession().setAttribute("msg","文章的标签数据加载失败");
            }else {
                while (rs.next()){
                    String[] data=new String[3];
                    //System.out.println(rs.getObject(1)+"|"+rs.getObject(2)+"|"+rs.getObject(3));
                    for(int i=0;i<data.length;i++){
                        if(rs.getObject(i+1)==null){
                            data[i]="";
                        }else {
                            data[i]=rs.getObject(i+1).toString();
                        }
                    }
                    for (String s:
                            data) {
                        System.out.print("|"+s+"|");
                    }
                    System.out.println();
                    essayLabelList.add(data);
                    System.out.println("文章标签数据加载成功");


                }
               /* for (String[] ss: essayLabelList) {
                    for (String s: ss) {
                        System.out.print("|"+s+"|");
                    }
                    System.out.println();
                }*/

            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            String  sql2="select  word\n" +
                    "from label\n" +
                    "ORDER BY count DESC LIMIT 0,5";
            ptmt=conn.prepareStatement(sql2);
            rs = ptmt.executeQuery();
            if(rs==null){
                System.out.println("文章标签数据加载失败");
                request.getSession().setAttribute("status","0");
                request.getSession().setAttribute("msg","热门标签数据加载失败");
            }else {
                int index=0;

                while (rs.next()){

                    topLabel[index]=rs.getString("word");
                    index++;
                }
                for (String s: topLabel) {
                    System.out.println(s);
                }
                    System.out.println("热门标题数据加载成功");


            }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            String  sql3="select  title\n" +
                    "from essay\n" +
                    "ORDER BY read_sum DESC LIMIT 0,5";
            ptmt=conn.prepareStatement(sql3);
            rs = ptmt.executeQuery();
            if(rs==null){
                System.out.println("文章标签数据加载失败");
                request.getSession().setAttribute("status","0");
                request.getSession().setAttribute("msg","热门文章数据加载失败");
            }else {
                int index=0;

                while (rs.next()){

                    topEssay[index]=rs.getString("title");
                    index++;
                }
                for (String s: topEssay) {
                    System.out.println(s);
                }
                System.out.println("热门文章数据加载成功");
            }


            rs.close();
            ptmt.close();
            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
/////////////////////////////////////////////////////////////////////////////
        if(essayList.isEmpty()||essayLabelList.isEmpty()||topEssay==null||topLabel==null){
            System.out.println("MainFlash加载失败");
            RequestDispatcher rd = request.getRequestDispatcher("index_in.jsp");
            rd.forward(request,response);
        }else {
            System.out.println("MainFlash加载完成");
            for(int i=0;i<5;i++){
                if(topLabel[i]==null){topLabel[i]="";}
                if(topEssay[i]==null){topEssay[i]="";}
            }


            request.getSession().setAttribute("status","1");
            request.getSession().setAttribute("msg","数据加载成功");
            request.getSession().setAttribute("essayList",essayList);
            request.getSession().setAttribute("essayLabelList",essayLabelList);
            request.getSession().setAttribute("topLabel",topLabel);
            request.getSession().setAttribute("topEssay",topEssay);

            //request.getSession().setAttribute("isFirst",1);
            RequestDispatcher rd = request.getRequestDispatcher("index_in.jsp");
            rd.forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
